﻿namespace SecurityGroupsAdministration.SecurityGroupsAdministration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;

    using Microsoft.SharePoint;

    public partial class SecurityGroupsAdministrationUserControl : UserControl
    {
        #region Methods

        protected void addField_Click( object sender, EventArgs e )
        {
            if ( securityGroupList.SelectedIndex == -1 )
                return;

            if ( departmentList.SelectedIndex == -1 )
                return;

            SecurityGroupsBL secgroupProcess = new SecurityGroupsBL( );
            string siteURL = SPContext.Current.Web.Url;

            while ( securityGroupList.SelectedItem != null )
            {
                selectedSecurity.Items.Add( securityGroupList.SelectedItem.Text );
                secgroupProcess.SaveSelectedGroup( siteURL, departmentList.SelectedItem.Text, securityGroupList.SelectedItem.Text );
                securityGroupList.Items.Remove( securityGroupList.SelectedItem );
            }
        }

        protected void departmentList_selectedIndexChanged( object sender, EventArgs e )
        {
            SecurityGroupsBL secgroupProcess = new SecurityGroupsBL( );
            List<StoreSecurityGroupsInformation> fixedDataListInformation = new List<StoreSecurityGroupsInformation>( );
            List<DepartmentSecurityGroup> departmentSecGroup = new List<DepartmentSecurityGroup>( );

            string siteURL = SPContext.Current.Web.Url;

            securityGroupList.Items.Clear( );

            secgroupProcess.LoadSecurityGroups( siteURL, departmentList.SelectedItem.Text.Trim( ) );
            fixedDataListInformation = secgroupProcess.SecurityGroups;

            securityGroupList.DataSource = fixedDataListInformation;
            securityGroupList.DataTextField = "SecurityGroup";
            securityGroupList.DataValueField = "SecurityGroup";
            securityGroupList.DataBind( );

            secgroupProcess.LoadSelectedSecurityGroups( siteURL, departmentList.SelectedItem.Text.Trim( ) );
            departmentSecGroup = secgroupProcess.DepartmentSecurityGroups;

            string targetDepartment = departmentList.SelectedItem.Text.Trim( );

            List<DepartmentSecurityGroup> filteredFieldResults = departmentSecGroup.FindAll(

            delegate( DepartmentSecurityGroup deptFields )
            {
                if ( deptFields.DepartmentName.Equals( targetDepartment ) )
                    return true;
                else
                    return false;
            }
            );

            selectedSecurity.DataSource = filteredFieldResults;
            selectedSecurity.DataTextField = "SecurityGroup";
            selectedSecurity.DataValueField = "SecurityGroup";
            selectedSecurity.DataBind( );
        }

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !this.IsPostBack )
            {
                DepartmentInformationBL departments = new DepartmentInformationBL( );
                List<DepartmentInformation> departmentListInformation = new List<DepartmentInformation>( );

                string siteURL = SPContext.Current.Web.Url;

                departments.LoadDepartments( siteURL, false );

                departmentListInformation = departments.DepartmentList;

                departmentList.DataSource = departmentListInformation;
                departmentList.DataTextField = "DepartmentName";
                departmentList.DataValueField = "ListName";
                departmentList.DataBind( );
            }
        }

        protected void removeField_Click( object sender, EventArgs e )
        {
            if ( departmentList.SelectedIndex == -1 )
                return;

            if ( selectedSecurity.SelectedIndex == -1 )
                return;

            SecurityGroupsBL secgroupProcess = new SecurityGroupsBL( );
            string siteURL = SPContext.Current.Web.Url;

            while ( selectedSecurity.SelectedItem != null )
            {
                securityGroupList.Items.Add( selectedSecurity.SelectedItem.Text );
                secgroupProcess.DeleteSelectedGroup( siteURL, departmentList.SelectedItem.Text, selectedSecurity.SelectedItem.Text );
                selectedSecurity.Items.Remove( selectedSecurity.SelectedItem );
            }
        }

        #endregion Methods
    }
}